#include <iostream>
#include <cmath>
using namespace std;
int happy[10000] ={0};
bool filter[10000] = {0};
int sumPow(int n)
{
    int sum = 0;
    do{
       int temp = n %10;
       n = n /10;
       sum  = sum + pow(temp,2);
    }while(n>0);
    return sum;
}
bool isHappy(int n)
{
    int a[400];
    int len = 0;
    int temp = n;
    while(true)
    {
        temp = sumPow(temp);
        if(temp == 1)
        {
            happy[n] = len+1;
            return true;
        }
        else
        {
            for(int i = 0; i<len; i++)
            {
                if(a[i] == temp)
                    return false;
            }
            a[len] = temp;
            len++;
        }
    }
}

bool isPrime(int n)
{
    for(int i = 2; i<n; i++)
    {
        if(n%i == 0)
           return false;
        
    }
    return true;
}
void filters(int n)
{
    int temp = n;
    while(true)
    {
        temp = sumPow(temp);
        if(temp == 1)
             break;
        else
            filter[temp] = true;
        
    }
}
int main()
{
   int A,B;
   cin>>A>>B; 
   for(int i = 1; i<=10000; i++)
       isHappy(i);

   for(int i = A; i<=B; i++)
   {
        if(happy[i]>0)
        {
           if(isPrime(i))
               happy[i] = happy[i] * 2;
        }
   }
   for(int i = A; i<=B; i++)
   {
       if(happy[i]>0)
           filters(i);
   }
   bool bIfEmpty = true;
   for(int i = A; i<=B; i++)
   {
        if(happy[i]>0 && !filter[i])
        {
           cout<<i<<" "<<happy[i]<<endl;
           bIfEmpty = false;
        }
   }
   if(bIfEmpty)
       cout<<"SAD"<<endl;
}